package com.geilizu.dom.controller;

import com.geilizu.dom.dto.BuildingDto;
import com.geilizu.dom.dto.SearchDto;
import com.geilizu.dom.entity.Building;
import com.geilizu.dom.entity.Student;
import com.geilizu.dom.result.ResponseCode;
import com.geilizu.dom.result.ResponseData;
import com.geilizu.dom.service.BuildingService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Building)表控制层
 *
 * @author makejava
 * @since 2022-07-12 16:59:02
 */
@RestController
@RequestMapping("building")
public class BuildingController {
    /**
     * 服务对象
     */
    @Resource
    private BuildingService buildingService;

    @GetMapping("/list")
    public ResponseData list(){
        return new ResponseData(ResponseCode.SUCCESS,buildingService.list());
    }


    @PostMapping("/save")
    public ResponseData save(@RequestBody Building building){
        boolean save = this.buildingService.save(building);
        if(!save){
            return new ResponseData(ResponseCode.DATAERROR);
        }
        return new ResponseData(ResponseCode.SUCCESS);
    }

    @GetMapping("/list/{page}/{size}")
    public ResponseData list(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Map<String,Object> map = this.buildingService.list(page, size);
        return new ResponseData("0","success", (Long) map.get("count"), map.get("buildingList"));
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchDto searchDto){
        Map<String,Object> map=buildingService.search(searchDto);
        return new ResponseData("200","success", (Long) map.get("count"),map.get("buildingList"));
    }
    @DeleteMapping("/deleteById/{id}")
    public ResponseData deleteById(@PathVariable("id") Integer id) {
        Integer del = this.buildingService.deleteById(id);
        if(del>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }
        return new ResponseData(ResponseCode.DATAERROR);
    }


    @PutMapping("/update")
    public ResponseData update(@RequestBody Building building){
        Integer update = this.buildingService.updateById(building);
        if(update>0){
            return new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FAILED);
        }

    }

    @GetMapping("/findById/{id}")
    public ResponseData findById(@PathVariable("id") Integer id) {
        BuildingDto buildingDto = buildingService.findById(id);
        return new ResponseData(ResponseCode.SUCCESS, buildingDto);
    }

}

